/*
 * Created on 24 janv. 2007
 */
package fr.mywiki.business.navigation;

import javax.servlet.http.HttpSession;

import fr.mywiki.business.account.AccountLite;
import fr.mywiki.business.login.LoginManager;
import fr.mywiki.business.profil.Profile;
import fr.mywiki.business.profil.ProfileManager;

/**
 * This manager deals with the navigation inside the application. Spaces are
 * managed here.
 * 
 * @author tlombard
 */
public class NavigationManager {
	/**
	 * Name of the session variable which indicates the name of the current space
	 * of the user
	 */
	public static final String CURRENT_SPACE = "current_space";

	/**
	 * Name of the session variable which indicates the name of the current page
	 * of the user
	 */
	public static final String CURRENT_PAGE = "current_page";

	/* Names of the spaces */
	/** Name of the wiki space */
	public static final String WIKI_SPACE = "wikispace";

	/** Name of the file management space */
	public static final String FILE_MANAGEMENT_SPACE = "filemanagementspace";

	/** Name of the models management space */
	public static final String MODELS_MANAGEMENTS_SPACE = "modelsManagementSpace";

	/** Name of the administration space */
	public static final String ADMIN_SPACE = "adminspace";

	/** This class cannot be instanced */
	private NavigationManager() {
	}

	/**
	 * Returns the space the user currently is in. If there is not current space,
	 * it is initialized.
	 * 
	 * @param session
	 *          The considered session, corresponding to the activity of a user.
	 * @return a <code>SpaceFlyweight</code> object representing the current
	 *         space.
	 */
	public static SpaceFlyweight getCurrentSpace(HttpSession session) {
		String space = getCurrentSpace0(session);

		// If space is null, it is initialized.
		if (space == null) {
			initSpace(session);
			space = getCurrentSpace0(session);
		}

		return getSpaces(session).getSpace(space);
	}

	/**
	 * Initializes the current space of the session with the first space of the
	 * spaces list given in parameter.
	 * 
	 * @param session
	 *          current session
	 */
	private static void initSpace(HttpSession session) {
		SpacesList spacesList = getSpaces(session);
		String spaceHome = spacesList.getSpaceName(0);
		setCurrentSpace(session, spaceHome);
	}

	/**
	 * Intern (and simplest) version of the access to current space.
	 * 
	 * @param session
	 *          current HttpSession.
	 * @return the current space's name.
	 */
	private static String getCurrentSpace0(HttpSession session) {
		return (String) session.getAttribute(CURRENT_SPACE);
	}

	/**
	 * Sets the new space in which the current session is.
	 * 
	 * @param session
	 *          The considered session.
	 */
	public static void setCurrentSpace(HttpSession session, String space) {
		session.setAttribute(CURRENT_SPACE, space);
	}

	/**
	 * Returns the list of spaces available for this session and 
	 * 
	 * @param session
	 *          the current connexion.
	 * @return a <code>SpacesList</code> object.
	 */
	public static SpacesList getSpaces(HttpSession session) {
		AccountLite account = LoginManager.getLoggedUser(session);
		if (account == null)
			return ProfileManager.getInstance().getSpaces(
					Profile.ANONYMOUS_PROFILE_ID);
		return account.getProfile().getSpaces();
	}
}
